package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.CarrierdMZ;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.Carrier;
import cn.eonshine.cuba.lsms.modular.basic.service.ICarrierClientService;
import cn.eonshine.cuba.lsms.modular.basic.service.ICarrierService;

@Service
public class CarrierClientService extends CommonService<CarrierdMZ> implements ICarrierClientService {
	@Autowired
	private ICarrierService carrierService;
	
	private final String listUrl = "/carrier/list";
	private final String updateUrl = "/carrier/update";
	
	@Override
	public void requestCarrier() {
		requestData(listUrl, updateUrl);
	}
	
	@Override
	protected Object dataHandle(CarrierdMZ carrier) throws Exception {
		QueryWrapper<Carrier> entity = new QueryWrapper<Carrier>();
		entity.eq("ID", carrier.getCode());
		Carrier info = carrierService.getOne(entity);
		boolean isInsert = false;
		if(info == null) {
			info = new Carrier();
			isInsert = true;
		}
		info.setId(carrier.getCode());
		info.setCreateBy(InfStatus.CREATE_BY);
		info.setUpdateBy(InfStatus.UPDATE_BY);
		info.setUpdateDate(InfStatus.setUpdate(carrier));
		info.setCreateDate(carrier.getCreateDate());
		info.setName(carrier.getName());
		info.setFullName(carrier.getFullName());
		info.setEnable(carrier.getEnable());
		info.setRemarks(carrier.getRemarks());
		
		if(isInsert) {
			carrierService.save(info);
		}else {
			carrierService.updateById(info);
		}
		return info;
	}
	
	@Override
	protected List<CarrierdMZ> feadBackDataHandle(Object obj, CarrierdMZ carrier, List<CarrierdMZ> feadBackDataLists) throws Exception {
		Carrier info = (Carrier) obj;
		CarrierdMZ tmpBack = new CarrierdMZ();
		tmpBack.setCode(carrier.getCode());
		tmpBack.setDataInputDatetime(carrier.getDataInputDatetime());
		tmpBack.setLsmsUpdate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		tmpBack.setId(info.getId());
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
}
